﻿using AutoMapper;
using MediatR;
using S.Archives.Api.Application.Command.Present;
using S.Archives.Domain.S.ArchivesModel;
using S.Archives.Infrstructre.InterFace;
using S.Common.Enums.Archives;

namespace S.Archives.Api.Application.CommandHandlers.Present
{
    public class PresentStatePLCommandHandler : IRequestHandler<PresentStatePLCommand, bool>
    {
        private readonly ISheepFileRepository sheepFileRepository;
        private readonly IMapper mapper;
        public PresentStatePLCommandHandler(ISheepFileRepository sheepFileRepository, IMapper mapper)
        {
            this.sheepFileRepository = sheepFileRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 批量动态修改在场状态
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<bool>  Handle(PresentStatePLCommand request, CancellationToken cancellationToken)
        {
            List<SheepFileModel> sheepFileModels = new List<SheepFileModel>();

            var sheep = sheepFileRepository.GetAll();
            foreach (var item in request.Present)
            {
                var a = sheep.FirstOrDefault(x => x.EarTag == item.EarTag);
                if (a != null)
                {
                    a.PresenceState = item.PresenceState;
                    a.UpdateDate = DateTime.Now;
                    sheepFileModels.Add(a);
                    

                }
                else
                {
                    return false;
                }

            }
            return await sheepFileRepository.UpdateRangeAsync(sheepFileModels);
        }
    }
}
